<template>
  <el-card>
    <el-row :gutter="20">
      <el-form ref="Bianjiref" label-position="top" :rules="Bianjirules" :inline="true" :model="formdata">
        <el-col :span="6"><el-form-item label="库区编号" prop="code">
          <el-input v-model="formdata.code" disabled placeholder="请输入" />
        </el-form-item></el-col>

        <el-col :span="6"><el-form-item label="所属仓库" prop="warehouseName">
          <el-select v-model="formdata.warehouseName" placeholder="请选择">
            <el-option
              v-for="item in dataList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"><el-form-item label="库区名称" prop="name">
          <el-input v-model="formdata.name" placeholder="请输入" />
        </el-form-item></el-col>
        <el-col :span="6"> <el-form-item label="温度类型" prop="temperatureType">
          <el-select v-model="formdata.temperatureType" placeholder="请选择">
            <el-option label="常温" value="CW" />
            <el-option label="冷藏" value="LC" />
            <el-option label="恒温" value="HW" />
          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"><el-form-item label="称重类型" prop="bearingType">
          <el-select v-model="formdata.bearingType" placeholder="请选择">
            <el-option label="重型" value="ZX" />
            <el-option label="轻型" value="QX" />

          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"><el-form-item label="用途属性" prop="useType">
          <el-select v-model="formdata.useType" placeholder="请选择">
            <el-option label="入库缓存区" value="RKHCQ" />
            <el-option label="出库缓存区" value="CKHCQ" />
            <el-option label="存储区" value="CCQ" />
            <el-option label="分拣区" value="FJQ" />
            <el-option label="质检区" value="ZJQ" />

          </el-select>
        </el-form-item></el-col>
        <el-col :span="6"> <el-form-item label="负责人" prop="personName">
          <el-input v-model="formdata.personName" placeholder="请输入" />
        </el-form-item></el-col>
        <el-col :span="6">   <el-form-item label="联系电话" prop="phone">
          <el-input v-model="formdata.phone" placeholder="请输入" />
        </el-form-item></el-col>
        <el-form-item label="库区状态" prop="radio">

          <el-radio v-model="formdata.status" :label="0">启用</el-radio>
          <el-radio v-model="formdata.status" :label="1">停用</el-radio>

        </el-form-item>

      </el-form>
      <el-button class="first" @click="$router.go(-1)">返回</el-button>
      <el-button type="primary" @click="Submit">提交</el-button>

    </el-row>

  </el-card>
</template>

<script>
import { nextBianhao, QuanBuList, AddKuQuList, BianjiKuQu, BianjiKuQuList } from '@/api'
export default {
  data() {
    return {
      warehouseName: [],
      Bjcurrent: '',
      // name: [],
      dataList: [],
      formdata: {
        code: '',
        warehouseName: this.dataList,
        name: '',
        bearingType: '',
        useType: '',
        personName: '',
        temperatureType: '',
        status: '',
        phone: '',
        warehouseId: ''
      },
      Bianjirules: {
        code: [
          { required: true }
        ],
        warehouseName: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入库区名称', trigger: 'blur' }
        ],
        bearingType: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        useType: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        temperatureType: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        personName: [
          { required: true, message: '请输入负责人姓名', trigger: 'blur' }
        ],

        phone: [{ pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' }]

      },
      radio: [
        { label: '启用', value: 0 },
        { label: '停用', value: 1 }
      ]
    }
  },
  computed: {
    title() {
      return this.Bjcurrent ? '编辑库区' : '新增库区'
    }
  },
  async created() {
    this.nextBianji()
    console.log(this.$route)
    this.$route.meta.title = this.Bjcurrent ? '编辑库区' : '新增库区'
    this.Bjcurrent = this.$route.query.id

    // console.log(this.Bjcurrent)
    if (this.Bjcurrent) {
      const res = await BianjiKuQu(this.Bjcurrent)
      console.log(res)
      this.formdata = res.data
      this.formdata.warehouseName = res.data.warehouseId
    }
  },
  methods: {
    async nextBianji() {
      const res = await nextBianhao()
      this.formdata.code = res.data
      const { data } = await QuanBuList()
      this.dataList = data
      console.log(data)

      // data.forEach(item => {
      //   this.name.push(item.name)
      // })
      // console.log(this.name)
    },
    Submit() {
      this.$refs.Bianjiref.validate(async(valid) => {
        if (!valid) return
        if (this.Bjcurrent) {
          await BianjiKuQuList(this.formdata)
        } else {
          const res = await AddKuQuList(this.formdata)
          console.log(res)
        }
        this.$router.go(-1)
        this.$message.success(this.title + '成功')
      })
    }
  }
}
</script>

<style lang="scss" scoped>
 ::v-deep.el-button{
  width: 170px;
  height: 40px;
  padding:1px 15px;

  margin: 16px;
  color: #000;
  border-radius: 20px;
  margin-right: 20px;
 }
 .first{
  margin-left: 300px;  ;
 }
</style>
